package com.aglhz.nature.presenter;

import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.AroundViewEvent;
import com.aglhz.nature.events.ShopListViewEvent;
import com.aglhz.nature.modle.AroundBean;
import com.aglhz.nature.modle.AroundSearchBean;
import com.aglhz.nature.modle.ShopListBean;
import com.aglhz.nature.modle.item.AroundData;
import com.aglhz.nature.modle.item.AroundSearchData;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.AroundView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;


public class AroundPresenter extends BasePresenter{
	private static String TAG = AroundPresenter.class.getName();
	private AroundSearchBean aroundSearchBean ;
	private List<AroundSearchData> mdata;
	// view
		private AroundView aroundView;
		
		// data
		private AroundBean aroundBean;
		private List<AroundData> data;
		public AroundPresenter(AroundView view) {
			super();
			aroundView = view;
		}
		
		// /*********************data operation*******************///
		public void loadData() {
			// load remote data
			
		aroundView.getLOcation();
			// save remote data
			
		}

		public void searchAroundShop(String editSearch){
			
			String url = ServerAPI.APIHEARD+ServerAPI.HOME_AROUND;
			 RequestParams params = new RequestParams();  
			    params.put("keyword",  editSearch);
			Log.e("---------url---",url);
			AsyncHttpClient client = new AsyncHttpClient();
			client.post(url,params, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						 AroundSearchBean slBean = gson.fromJson(responseString,
								 AroundSearchBean.class);
						 aroundSearchBean= (AroundSearchBean) slBean.clone();
						
						Log.d(TAG,"ShopListView---111-----"+aroundSearchBean.getOther());	
						mdata = aroundSearchBean.getData();
						Log.d(TAG,"ShopListView---mcp-----"+aroundSearchBean.getData());	
						 // send event
						 AroundViewEvent event = new   AroundViewEvent(
								 AroundViewEvent.EVENT_UPDATE_AROUNDSEARCH_LISTVIEW);
						 EventBus.getDefault().post(event);
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			

		

		
			});
			
		}
		// /*********************view operation*******************///
		private void showListView() {
			aroundView.showAroundList(aroundBean.getData());
			
		}
		
		public void showSearchListView() {
			// TODO Auto-generated method stub
			aroundView.showAroundSearchList(aroundSearchBean.getData());
		}
		
		///********************view operation*******************///
		@Subscribe(threadMode = ThreadMode.MAIN)
		public void onEventAroundThread(AroundViewEvent event) {

			String msg = event.getMessage();
			Log.i(TAG, " AroundViewEvent:" + msg);
			// update UI By event
			if ( AroundViewEvent.EVENT_UPDATE_AROUND_LISTVIEW == msg) {
				// update grid view
				this.showListView();
			}else if ( AroundViewEvent.EVENT_UPDATE_AROUNDSEARCH_LISTVIEW == msg) {
				// update grid view
				this.showSearchListView();
			} 
			

		}
		
		
		
		
		

		

		public void loadAroundBean(double d,double f) {
			
			String url = ServerAPI.APIHEARD+ServerAPI.HOME_AROUND+"?lat="+d+"&lng="+f;
			Log.e("AroundBean", url);
			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						 AroundBean cBean = gson.fromJson(responseString,
								 AroundBean.class);
						 aroundBean= (AroundBean) cBean.clone();
						
						Log.d(TAG,"AroundBean---111-----"+aroundBean.getOther());	
						data = aroundBean.getData();
						Log.d(TAG,"AroundBean---mcp-----"+aroundBean.getData());	
						 // send event
						 AroundViewEvent event = new   AroundViewEvent(
								 AroundViewEvent.EVENT_UPDATE_AROUND_LISTVIEW);
						 EventBus.getDefault().post(event);
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			

		

		
			});

			
		}
		//定位失败
	public void loadAroundBean() {
			
			String url = ServerAPI.APIHEARD+ServerAPI.HOME_AROUND;

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						 AroundBean cBean = gson.fromJson(responseString,
								 AroundBean.class);
						 aroundBean= (AroundBean) cBean.clone();
						
						Log.d(TAG,"AroundBean---111-----"+aroundBean.getOther());	
						data = aroundBean.getData();
						Log.d(TAG,"AroundBean---mcp-----"+aroundBean.getData());	
						 // send event
						 AroundViewEvent event = new   AroundViewEvent(
								 AroundViewEvent.EVENT_UPDATE_AROUND_LISTVIEW);
						 EventBus.getDefault().post(event);
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			

		

		
			});

			
		}

}
